package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallSeatMapper;
import org.linlinjava.litemall.db.dao.LitemallStoreMapper;
import org.linlinjava.litemall.db.dao.StoreMapper;
import org.linlinjava.litemall.db.dao.TagMapper;
import org.linlinjava.litemall.db.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:	商家管理
 * @Author:			Coach tam
 * @Company:		坚持灵活  灵活坚持
 * @CreateDate:		2021-4-7 20:45:45
 */
@Service
@Transactional
public class LitemallStoreService {
    LitemallStore.Column[] columns = new LitemallStore.Column[]{LitemallStore.Column.id,LitemallStore.Column.picUrl,LitemallStore.Column.shortDescription,LitemallStore.Column.name, LitemallStore.Column.recommendIndex,LitemallStore.Column.avgConsumer,LitemallStore.Column.jcBeginTime,LitemallStore.Column.jcEndTime,LitemallStore.Column.images};
    @Autowired
    private LitemallStoreMapper storeMapper;
    @Autowired
    private StoreMapper myStoreMapper;
    @Autowired
    private TagMapper tagMapper;


    @Transactional(readOnly = true)
    public LitemallStore findById(Integer id) {
        LitemallStoreExample example = new LitemallStoreExample();
        example.createCriteria().andIdEqualTo(id);
        return storeMapper.selectOneByExampleWithBLOBs(example);
    }


    @Transactional
    public void add(LitemallStoreVo entity) {
        entity.setAddTime(LocalDateTime.now());
        entity.setUpdateTime(LocalDateTime.now());
        entity.setBalance(0l);
        storeMapper.insertSelective(entity);
        insertStoreTagMapping(entity);
    }

    public int updateById(LitemallStoreVo entity) {

        //先删除store_tag_mapping中间表数据
        Integer storeId = entity.getId();
        tagMapper.deleteByStoreId(storeId);

        insertStoreTagMapping(entity);

        entity.setUpdateTime(LocalDateTime.now());
        return storeMapper.updateByPrimaryKeySelective(entity);
    }

    private void insertStoreTagMapping(LitemallStoreVo entity) {
        Integer storeId = entity.getId();
        String[] tags = entity.getTags();
        if (tags != null && tags.length > 0) {
            for (String tagId : tags) {
                tagMapper.insertStoreTagMapping(storeId,tagId);
            }
        }
    }

    @Transactional(readOnly = true)
    public List<LitemallStore> querySelective(String keyword,Integer page, Integer size, String sort, String order) {
        LitemallStoreExample example = new LitemallStoreExample();
        LitemallStoreExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(keyword)) {
            criteria.andNameLike("%" + keyword + "%");
        }

        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return storeMapper.selectByExampleWithBLOBs(example);
    }
    @Transactional(readOnly = true)
    public List<LitemallStore> querySelective(String tag, String keyword, Integer page, Integer size, String sort, String order) {
        PageHelper.startPage(page, size);
        return myStoreMapper.selectByTag(tag,keyword,page,size,sort,order);
    }
    @Transactional(readOnly = true)
    public int count() {
        LitemallStoreExample example = new LitemallStoreExample();
        example.or().andDeletedEqualTo(false);

        return (int) storeMapper.countByExample(example);
    }

    public void deleteById(Integer id) {
        storeMapper.logicalDeleteByPrimaryKey(id);
    }
    @Transactional(readOnly = true)
    public List<LitemallStore> findStore(String name) {
        LitemallStoreExample example = new LitemallStoreExample();
        example.or().andNameEqualTo(name).andDeletedEqualTo(false);
        return storeMapper.selectByExample(example);
    }

    @Transactional(readOnly = true)
    public LitemallStore findByUserName(String username) {
        LitemallStoreExample example = new LitemallStoreExample();
        example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        return storeMapper.selectOneByExample(example);
    }
    @Transactional(readOnly = true)
    public List<LitemallStore> queryRecommendStoreList(int offset, int limit) {
        LitemallStoreExample example = new LitemallStoreExample();
        example.or().andIsRecommendedEqualTo(true).andDeletedEqualTo(false);
        example.setOrderByClause("sort_time desc");
        PageHelper.startPage(offset, limit);
        return storeMapper.selectByExampleSelective(example, columns);
    }

    @Transactional(readOnly = true)
    public Long queryBalance(Integer storeId) {
        LitemallStore.Column[] columns = new LitemallStore.Column[]{LitemallStore.Column.balance};
        LitemallStore litemallStore = storeMapper.selectByPrimaryKeySelective(storeId, columns);
        Long balance = litemallStore.getBalance();
        return balance;
    }

    @Transactional(readOnly = true)
    public List<LitemallStore> selectByExample(LitemallStoreExample example) {
        return storeMapper.selectByExample(example);
    }

    @Transactional(readOnly = true)
    public List<SeatVo> findSeatList(Integer storeId, String type, Integer orderRange) {
        List<Integer> okSeatIdList = myStoreMapper.selectSeatIdList(storeId, type, orderRange).stream().map(LitemallSeat::getId).collect(Collectors.toList());
        List<SeatVo> allSeatList = myStoreMapper.selectAllSeatList(storeId, type, orderRange);
        //标识哪些是可预定的座位
        allSeatList.forEach(seatVo -> seatVo.setCanOrder(okSeatIdList.contains(seatVo.getId())));
        return allSeatList;
    }
}
